package xyz.sicau.blog.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.sicau.blog.bean.Note;
import xyz.sicau.blog.dao.NoteMapper;
import xyz.sicau.blog.dao.UserMapper;
import xyz.sicau.blog.utils.JwtUtils;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "记录接口")
@RequestMapping("/note")
public class NoteController {
    @Resource
    NoteMapper noteMapper;
    @Resource
    UserMapper userMapper;

    // 通过用户名分页查询notes（不分页），注意：过滤了tag
    @RequestMapping("/notes")
    public List<Note> getNotePage(String userToken) {
        int userId = JwtUtils.getUserIdByToken(userToken);
//        System.out.println(userId);
        return noteMapper.getNotes(userId);
    }

    // 通过用户名分页查询notes，注意：过滤了tag
    @RequestMapping("/notes-page")
    public List<Note> getNotePage(int start, int page, String userToken) {
        int userId = JwtUtils.getUserIdByToken(userToken);
        return noteMapper.getNotePage(start, page, userId);
    }

    // 添加note，回传的是note
    @RequestMapping("/release")
    public Note releaseNote(String userToken, String noteJson) {
        int userId = JwtUtils.getUserIdByToken(userToken);
        String userName = userMapper.getUserNameById(userId);

        Note note = JSON.parseObject(noteJson, Note.class);
        note.setNoteUserid(userId);
        note.setNoteUsername(userName);

        noteMapper.insertNote(note);
        System.out.println("/note/release：" + note);
        return note;
    }

    // 通过id删除note
    @RequestMapping("/delete")
    public int releaseNote(String userToken, int noteId) {
        int userId = JwtUtils.getUserIdByToken(userToken);
        // userid用于检验
        System.out.println("/note/delete：" + userId + " 删除日记，id：" + noteId);
        return noteMapper.deleteNote(noteId, userId);
    }

    // 修改数据========================================================================

    @RequestMapping("/update")
    public int updateRelation(String userToken, String noteJson) {
        Note note = JSON.parseObject(noteJson, Note.class);
        note.setNoteUserid(JwtUtils.getUserIdByToken(userToken));

        System.out.println("/note/update：" + note);
        return noteMapper.updateNote(note);
    }

    // Tag================================================
    // 通过用户名查询全部tags
    @RequestMapping("/tags_all")
    public List<Note> getTagsAll(String userToken) {
        int userId = JwtUtils.getUserIdByToken(userToken);
        return noteMapper.getTagsAll(String.valueOf(userId));
    }

    // Todo================================================
    // 通过用户名查询全部todos
    @RequestMapping("/todos_all")
    public List<Note> getTodosAll(String userToken) {
        int userId = JwtUtils.getUserIdByToken(userToken);
        return noteMapper.getTodosAll(String.valueOf(userId));
    }


    // 分享 ============================================
    // 查分享的note，通过id 查询type为public的note
    @RequestMapping("/notes_share")
    public List<Note> getNotesShare(String ids) {
        System.out.println("/note/notes_share: " + ids);
        return noteMapper.getNotesShare(ids.split(","));
    }

    // type 批量改，先不校验，用于分享功能
    @RequestMapping("/set_types")
    public int setTypes(String userToken, String ids, String value) {
        int userId = JwtUtils.getUserIdByToken(userToken);
        System.out.println(ids.split(",")[0]);
        return noteMapper.updateNoteTypes(ids.split(","), value, userId);
    }
}
